//
//  WKUniversal.m
//  WeKeTang
//
//  Created by ichina on 2017/8/22.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKUniversal.h"
#import "NSString+Addition.h"
#import "sys/utsname.h"
#import <sys/sysctl.h>
#import <net/if.h>
#import <net/if_dl.h>
#import<CommonCrypto/CommonDigest.h>
@implementation WKUniversal

+ (void)saveImgData:(NSData*)imgData{
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [doc stringByAppendingPathComponent:@"avaderImg.plist"];
    
    NSDictionary *dic = @{@"imgData": imgData
                          };
    BOOL succesee = [dic writeToFile:path atomically:YES];
    
    if (succesee == 1) {
        
        NSLog(@"保存头像成功");
    }else{
        NSLog(@"保存头像失败");
    }
    
}
+ (NSData*)getImgData{
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [doc stringByAppendingPathComponent:@"avaderImg.plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    
    return dict[@"imgData"];
    
}
+ (void)removeImgData{
    
    NSFileManager* fileManager=[NSFileManager defaultManager];
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [doc stringByAppendingPathComponent:@"avaderImg.plist"];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:path];
    if (!blHave) {
        NSLog(@"没找到文件");
        return ;
    }else {
        NSLog(@" 找到文件");
        BOOL blDele= [fileManager removeItemAtPath:path error:nil];
        if (blDele) {
            NSLog(@"删除本地头像成功");
        }else {
            NSLog(@"删除本地头像失败");
        }
    }
}
#pragma mark - 获得系统信息
/*得到当前设置的语言*/
+ (NSString*)currentLanguage {
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSArray* languages = [defaults objectForKey:@"AppleLanguages"];
    NSString* currentLanguage = [languages objectAtIndex:0];
    return currentLanguage;
}
//获取版本信息
+ (NSString *)getDeviceInfo {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    return deviceString;
}

/*手机号码验证 */
+ (NSString*)validateMobile:(NSString *)mobile {
    NSString* tyle = nil;
    if(mobile==nil || [mobile length]<1){
        return @"请输入手机号";
    }
    if([mobile length]!=11 || ![mobile hasPrefix:@"1"]){
        return @"手机号不合法";
    }
    const char* phoneChar = [mobile UTF8String];
    int i = 0;
    while(*(phoneChar + i) != '\0'){
        char ch = *(phoneChar + i);
        if(!isdigit(ch)){
            return @"手机号不合法";
        }
        i++;
    }
    return tyle;
}
+ (void)showInfo:(NSString*)info{
    
    YYLabel *label = [[YYLabel alloc]init];
    label.text = info;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = UIColor.whiteColor;
    label.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
    label.layer.cornerRadius = 5;
    label.layer.masksToBounds = YES;
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:16];
    CGSize size = [label.text bd_sizeWithFont:[UIFont systemFontOfSize:18] textMaxSize:CGSizeMake(kDeviceScreenWidth, 300)];
    
    label.size = CGSizeMake(size.width, size.height*2);
    label.center =[UIApplication sharedApplication].keyWindow.center;
    [[UIApplication sharedApplication].keyWindow addSubview:label];
    label.transform = CGAffineTransformMakeScale(1.2, 1.2);
    [UIView animateWithDuration:1.5 animations:^{
        
        label.alpha = 0;
        
    }completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.25 animations:^{
            
            label.transform = CGAffineTransformIdentity;
            [label removeFromSuperview];
        }];
    }];
}
//图片转换MD5
+(NSString*)getFileMD5AtImageData:(NSData*)imageData{
    
    CC_MD5_CTX md5;
    
    CC_MD5_Init(&md5);
    
    CC_MD5_Update(&md5, [imageData bytes], (CC_LONG)[imageData length]);
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5_Final(digest, &md5);
    
    NSString* result = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                        digest[0], digest[1],
                        digest[2], digest[3],
                        digest[4], digest[5],
                        digest[6], digest[7],
                        digest[8], digest[9],
                        digest[10], digest[11],
                        digest[12], digest[13],
                        digest[14], digest[15]];
    
    return result;
}

/**把时间戳转换成格式为yyyy年M月dd的string*/
+ (NSString *)intervalToDate:(long long int) interval withFormat:(NSString *)format {
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval / 1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    return [formatter stringFromDate:date];
}
//获取时间戳
+ (NSString *)getTimeStap {
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970]*1000;
    NSString *timeString = [NSString stringWithFormat:@"%0.f", a];
    return timeString;
}
//处理数据
+(void)saveDefaultValue:(id)value key:(NSString *)key {
    if (value) {
        [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
    }
}
+(id)readDefaultValue:(NSString *)key {
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}
+ (NSString *)macaddress{
    
    int                mib[6];
    size_t              len;
    char                *buf;
    unsigned char      *ptr;
    struct if_msghdr    *ifm;
    struct sockaddr_dl  *sdl;
    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;
    
    if ((mib[5] = if_nametoindex("en0")) == 0) {
        printf("Error: if_nametoindex error/n");
        return NULL;
    }
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1/n");
        return NULL;
        
    }
    if ((buf = malloc(len)) == NULL) {
        printf("Could not allocate memory. error!/n");
        return NULL;
    }
    
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        return NULL;
        
    }
    
    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    
    NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    
    //    NSString *outstring = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    
    //    NSLog(@"outString:%@", outstring);
    free(buf);
    return [outstring uppercaseString];
    
}

@end
